(function ($) {
  var novel_id, readingLink;

  function bindEvents() {
    $('#favor_btn').on('click', function () {
      service.favor(novel_id, function () {
        showToast();
      }, function () {
        console.log("whitelist: failed to favor novel, it's id:", novel_id);
      })
    })
    $('#reading_btn').on('click', function () {
      if (readingLink)
        location.href = readingLink
    })
  }

  function showToast() {
    $('.toast').show();
    setTimeout(function () {
      $('.toast').hide()
    }, 2000);
  }

  function initView(detail) {
    $('#novel_name').text(detail.name);
    $('#author').text(detail.author);
    if (detail.status.trim()) {
      $('#status').text(detail.status.trim());
    }
    $('#word_count').text(util.shortNumber(detail.word_count));
    $('#source').text(detail.source);
    $('#views').text(util.shortNumber(detail.views));
    $('#genre').text(detail.genre + '类');
    if (detail.rating) {
      $('#rating').css('background', 'none');
      $('#rating > span').text(detail.rating);
    }
    if (detail.ranking) {
      $('#rank').css('background', 'none');
      $('#rank > span').text(detail.ranking);
    }

    if (detail.third_party_ratings.length > 0) {
      var elem = $('#third_party_chart').show();
      showChart(elem.get(0), detail.third_party_ratings);
    }
  }

  function showChart(elem, ratings) {
    var thirdPartyNames = [], thirdPartyRatings = [], thirdPartyMeanRatings = [];
    for (var i = 0; i < ratings.length; ++i) {
      thirdPartyNames.push(ratings[i].third_party_name);
      thirdPartyRatings.push(Number(ratings[i].third_party_rating));
      thirdPartyMeanRatings.push(Number(ratings[i].third_party_mean_rating));
    }

    var rootSize = parseInt($('html').css('fontSize')),
        hSpace = 0.3 * rootSize + 'px',
        wSpace = 0.2 * rootSize + 'px',
        small = 0.25 * rootSize,
        middle = 0.3 * rootSize,
        large = 0.5 * rootSize;

    var option = {
      backgroundColor: '#fff',
      title: {
        text: '第三方评分',
        left: wSpace,
        top: hSpace,
        textStyle: {
          color: '#555',
          fontSize: middle
        }
      },
      tooltip: {},
      legend: {
        top: hSpace,
        right: wSpace,
        textStyle: {
          color: '#555',
          fontSize: small
        },
        data: ['该类均分', '本书评分']
      },
      xAxis: {
        data: thirdPartyNames
      },
      yAxis: {
        type: 'value',
        min: 0,
        max: 10
      },
      series: [{
        name: '该类均分',
        type: 'bar',
        barMaxWidth: large,
        itemStyle: {
          normal: {
            color: '#dde7ea'
          }
        },
        barGap: '-30%',
        data: thirdPartyMeanRatings
      }, {
        name: '本书评分',
        type: 'bar',
        barMaxWidth: large,
        itemStyle: {
          normal: {
            color: '#8dcee4'
          }
        },
        data: thirdPartyRatings
      }]
    };

    var chart = echarts.init(elem);
    chart.setOption(option);
  }

  $(document).on('ready', function () {
    var parts = location.href.split('?');
    if (parts.length > 0) {
      novel_id = parts[1].split('=')[1];
    }
    service.init();
    service.getNovelDetail(novel_id, function (data) {
      bindEvents();
      readingLink = data.source_url;
      initView(data);
    }, function () {
      console.log("whitelist: fail to get novel detail, the novel's id is:", novel_id);
    })
  })
})(Zepto);